//
//  CollectionData.m
//  NewSelf
//
//  Created by fred on 12-11-18.
//  Copyright (c) 2012年 fred. All rights reserved.
//

#import "CollectionData.h"

@implementation CollectionData

// plist 的文件名和第一级 key
+ (NSString *)getPlistName:(kCollectionType)type {
    NSString *plistName = nil;
    switch (type) {
        case kCollection_playing: {
            plistName = @"kCollection_playing";
            break;
        }
        case kCollection_played: {
            plistName = @"kCollection_played";
            break;
        }
        default:
            break;
    }
    return plistName;
}
// 判断是否已经收藏
+ (BOOL)isExisted:(NSDictionary *)collection type:(kCollectionType)type {
    BOOL isExisted = NO;
    // 获取收藏的字典
    NSMutableDictionary *collections = [self getCollections:type];
    // 如果为空，直接返回 false
    if (!collections) {
        return isExisted;
    }
    
    // 获取收藏的数组并判断 id 是否相等，如果相等的话，说明已经存在
    NSArray *collectionArray = [collections objectForKey:[self getPlistName:type]];
    for (int i = 0; i < [collectionArray count]; i++) {
        if ([[collection objectForKey:@"item_id"] isEqualToString:[[collectionArray objectAtIndex:i] objectForKey:@"item_id"]]) {
            isExisted = YES;
            break;
        }
    }
    
    return isExisted;
}
// 添加收藏
+ (BOOL)addToCollection:(NSDictionary *)collection type:(kCollectionType)type {
    // 默认失败
    BOOL isSuccess = NO;
    if (collection && [[collection allKeys] count] && [collection objectForKey:@"item_id"]) {
        if (![self isExisted:collection type:type]) {
            // 获取字典
            NSMutableDictionary *collections = [self getCollections:type];
            if (!collections) {
                collections = [[[NSMutableDictionary alloc] init] autorelease];
            }
            // 获取数组
            NSMutableArray *collectionArray = [collections objectForKey:[self getPlistName:type]];
            if (!collectionArray) {
                collectionArray = [[[NSMutableArray alloc] init] autorelease];
                [collections setObject:collectionArray forKey:[self getPlistName:type]];
            }
            // 添加数据
            [collectionArray addObject:collection];
            
            // 重新保存到文件中
            isSuccess = [self setCollections:collections type:type];
        }
    }
    
    // 发送通知
    //    if (isSuccess) {
    //        [[NSNotificationCenter defaultCenter] postNotificationName:AddToCollectionNotification object:nil];
    //    }
    
    return isSuccess;
}
// 删除收藏
+ (BOOL)deleteFromCollection:(NSDictionary *)collection type:(kCollectionType)type {
    // 默认成功
    BOOL isSuccess = YES;
    // 获取字典
    NSMutableDictionary *collections = [self getCollections:type];
    if (!collections) {
        return isSuccess;
    }
    // 获取数组
    NSMutableArray *collectionArray = [collections objectForKey:[self getPlistName:type]];
    if (!collectionArray) {
        return isSuccess;
    }
    for (int i = 0; i < [collectionArray count]; i++) {
        if ([[collection objectForKey:@"item_id"] isEqualToString:[[collectionArray objectAtIndex:i] objectForKey:@"item_id"]]) {
            [collectionArray removeObjectAtIndex:i];
            // 重新保存到文件中
            isSuccess = [self setCollections:collections type:type];
            break;
        }
    }
    
    // 发送通知
    //    if (isSuccess) {
    //        [[NSNotificationCenter defaultCenter] postNotificationName:DeleteFromCollectionNotification object:nil];
    //    }
    
    return isSuccess;
}
// 获取所有收藏
+ (NSMutableDictionary *)getCollections:(kCollectionType)type {
    NSString *plistName = [NSString stringWithFormat:@"%@.plist", [self getPlistName:type]];
    NSString *plishPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:plistName];
    NSMutableDictionary *collections = [NSMutableDictionary dictionaryWithContentsOfFile:plishPath];
    
    return collections;
}
// 获取收藏，返回数组
+ (NSMutableArray *)getArrayData:(kCollectionType)type {
    if ([self hasContent:type]) {
        return [[self getCollections:type] objectForKey:[self getPlistName:type]];
    }
    return nil;
}
// 替换收藏的内容
+ (BOOL)setCollections:(NSDictionary *)collections type:(kCollectionType)type {
    // 默认成功
    BOOL isSuccess = YES;
    NSString *plistName = [NSString stringWithFormat:@"%@.plist", [self getPlistName:type]];
    NSString *plishPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:plistName];
    isSuccess = [collections writeToFile:plishPath atomically:YES];
    return isSuccess;
}
// 是否有数据
+ (BOOL)hasContent:(kCollectionType)type {
    BOOL hasContent = NO;
    if ([self getCollections:type] && [[[self getCollections:type] objectForKey:[self getPlistName:type]] count]) {
        hasContent  = YES;
    }
    return hasContent;
}
@end